var EntityHandling = {
	saveEntity: function(successFunc){
		if ($.data(document,"save.disabled") == false || $.data(document,"save.disabled") == undefined ){
			$.data(document,"save.disabled",true);

			var form = $(".ui-tabs-panel:not(ui-tabs-hide) form");
			var url = $(".ui-tabs-panel:not(ui-tabs-hide) form").attr("action");
			
			$.post(url, form.serialize(),null,"json")
			.success(function(data) { 
				Messages.showAlert(data.message);
				
				EntityHandling.afterSave();
				
				if (successFunc != undefined){
					successFunc(data.entity);
				}
			})
			.error(function(data){
				Messages.showValidations(data);
			}).complete(function(){
				$.data(document,"save.disabled",false);
			});
		}
	},
	saveDialogEntity: function(successFunc){	
		var form = $(".entityDialog form");
		var url = $(form).attr("action");
		
		$.post(url, form.serialize(),null,"json")
		.success(function(data) { 
			Messages.showAlert(data.message);
			if (successFunc != undefined){
				successFunc(data.entity);
			}
		})
		.error(function(data){
			Messages.showDialogValidations(data);
		});
	},
	afterSave: function(){
		var size = $(".tab").tabs( "length" );
		if (size != 0){
			var index = $(".tab").tabs('option', 'selected');
			$(".tab").tabs( "remove" ,index);
			$('.tab').tabs("option","disabled", []);
			$(".tab").tabs( "select" ,0);
		}
	}
};